package com.party.admin.biz.crowdfund;

import com.google.common.base.Function;
import com.google.common.collect.Maps;
import com.party.admin.biz.activity.ActivityBizService;
import com.party.admin.biz.file.FileBizService;
import com.party.admin.biz.system.MemberBizService;
import com.party.admin.utils.RealmUtils;
import com.party.admin.web.dto.output.crowdfund.ListForTargetOutput;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.redis.StringJedis;
import com.party.common.utils.LangUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.crowdfund.CrowdfundEvent;
import com.party.core.model.crowdfund.ProjectWithAuthor;
import com.party.core.model.distributor.WithCount;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.crowdfund.ICrowdfundEventService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ISupportTransferService;
import com.party.core.service.distributor.IDistributorRelationService;
import com.party.core.service.order.IOrderFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 联合发起业务接口
 * Created by wei.li
 *
 * @date 2017/3/2 0002
 * @time 14:12
 */

@Service
public class RepresentBizService {

	@Autowired
	private IDistributorRelationService distributorRelationService;
	@Autowired
	private StringJedis stringJedis;
	@Autowired
	private IActivityService activityService;
	@Autowired
	private IProjectService projectService;
	@Autowired
	private IOrderFormService orderFormService;
	@Autowired
	private ISupportTransferService supportTransferService;
	@Autowired
	private ProjectBizService projectBizService;
	@Autowired
	private ActivityBizService activityBizService;
	@Autowired
	private ICrowdfundEventService crowdfundEventService;
	@Autowired
	private FileBizService fileBizService;
	@Autowired
	private CosBizService cosBizService;
	@Autowired
	private MemberBizService memberBizService;

	@Value("#{party['mic.url']}")
	private String micUrl;

	/**
	 * 项目下联合发起
	 * 
	 * @param page 	分页参数
	 * @param withCount 联合发起信息
	 * @param id 编号
	 * @return 联合发起列表
	 */
	public List<ListForTargetOutput> listForTarget(Page page, WithCount withCount, String id) {
		String memberId = RealmUtils.getCurrentUser().getId();
		if (null == withCount){
			withCount = new WithCount();
		}
		withCount.setTargetId(id);
		List<WithCount> distributorRelationList = distributorRelationService.listWithCount(withCount, page);

		List<ListForTargetOutput> list
				= LangUtils.transform(distributorRelationList, new Function<WithCount, ListForTargetOutput>() {
			public ListForTargetOutput apply(WithCount distributorRelation) {
				//替换手机号
				String phone = memberBizService.replacePhone(memberId, distributorRelation.getAuthorMobile());
				distributorRelation.setAuthorMobile(phone);

				ListForTargetOutput listForTargetOutput = ListForTargetOutput.transform(distributorRelation);
				listForTargetOutput.setFavorerNum(distributorRelation.getFavorerNum());
				listForTargetOutput.setActualAmount(distributorRelation.getFavorerAmount());
				listForTargetOutput.setCrowdfundNum(distributorRelation.getCrowdfundNum());
				String filePath = fileBizService.getUploadCIPath("represent",distributorRelation.getDistributorId());
				String codeUrl = cosBizService.getPreviewPictureUrl(filePath + distributorRelation.getId());
				String content = "project/represent_target.html?id="+id+"&parentId="+distributorRelation.getParentId()+"&distributorId="+distributorRelation.getDistributorId();
				listForTargetOutput.setQrCodeUrl(codeUrl);
				listForTargetOutput.setUrl(micUrl + content);
				return listForTargetOutput;
			}
		});
		return list;
	}


	/**
	 * 根据事件编号查询
	 *
	 * @param page 分页参数
	 * @param withCount 查询参数
	 * @return 输出视图
	 */
	public List<ListForTargetOutput> listForEvent(Page page, WithCount withCount){
		String memberId = RealmUtils.getCurrentUser().getId();
		List<WithCount> distributorRelationList = distributorRelationService.listWithCount(withCount, page);
		List<ListForTargetOutput> list
				= LangUtils.transform(distributorRelationList, new Function<WithCount, ListForTargetOutput>() {
			public ListForTargetOutput apply(WithCount distributorRelation) {
				//替换手机号
				String phone = memberBizService.replacePhone(memberId, distributorRelation.getAuthorMobile());
				distributorRelation.setAuthorMobile(phone);

				ListForTargetOutput listForTargetOutput = ListForTargetOutput.transform(distributorRelation);
				listForTargetOutput.setFavorerNum(distributorRelation.getFavorerNum());
				listForTargetOutput.setActualAmount(distributorRelation.getFavorerAmount());
				listForTargetOutput.setCrowdfundNum(distributorRelation.getCrowdfundNum());
				return listForTargetOutput;
			}
		});
		return list;
	}

	/**
	 * 联合发起列表
	 *
	 * @param id        活动编号
	 * @param page      分页参数
	 * @param withCount 查询参数
	 * @return 输出视图
	 */
	public Map<String, Object> listForActivityCommon(Page page, WithCount withCount, String id) {
		Map<String, Object> map = Maps.newHashMap();
		page.setLimit(20);
		Activity activity = activityService.get(id);
		map.put("activity", activity);

		List<ListForTargetOutput> listForTargetOutputList = listForTarget(page, withCount, id);
		Integer crowdfundNum = projectService.sizeForTargetId(id);

		//众筹金额
		float actualAmount = activityBizService.actualAmountForTargetId(id);


		//获取转入的人
		Integer transferNum = supportTransferService.getShiftToPeopleTotal(activity.getId());

		map.put("transferNum", transferNum == null ? 0 : transferNum);
		map.put("actualAmount", actualAmount);
		map.put("crowdfundNum", crowdfundNum);
		ProjectWithAuthor projectWithAuthor = new ProjectWithAuthor();
		projectWithAuthor.setTargetId(id);
		projectWithAuthor.setPayType(1);
		map.put("directPayNum", projectBizService.projectForActivityList(projectWithAuthor, null).size()); //直接付款众筹数量
		map.put("list", listForTargetOutputList);
		map.putAll(activityBizService.putZipInfo(Constant.PRE_ZIP_URL + "represent" + id));
		map.put("page", page);
		return map;
	}

	/**
	 * 根据事件查询联合发起数据
	 *
	 * @param withCount 查询参数
	 * @param page      分页参数
	 * @return 交互数据
	 */
	public Map<String, Object> listForEventCommon(Page page, WithCount withCount) {
		Map<String, Object> map = Maps.newHashMap();

		page.setLimit(20);
		List<ListForTargetOutput> listForTargetOutputList = listForEvent(page, withCount);
		Integer projectNum = projectService.countForEvent(withCount.getEventId());
		Integer dpProjectNum = projectService.dpCountForEvent(withCount.getEventId(), null);
		map.put("list", listForTargetOutputList);
		map.put("projectNum", projectNum);
		map.put("representNun", page.getTotalCount());
		map.put("directNun", dpProjectNum);
		map.put("withCount", withCount);
		map.put("page", page);
		CrowdfundEvent crowdfundEvent = crowdfundEventService.get(withCount.getEventId());
		map.put("crowdfundEvent", crowdfundEvent);
		return map;
	}
}
